from PyQt5.QtGui import QIcon
from manager.comManager import *


class TabSensor(object):
    _instance_lock = threading.Lock()
    _first_init = True

    def __init__(self):
        if self._first_init:
            self.ui = None
            self._first_init = False
            self.str_send = "AT+CIPSEND" + "\r\n"
            self.str_not_send = "+++"
        pass

    def __new__(cls, *args, **kwargs):
        if not hasattr(cls, '_instance'):
            with TabSensor._instance_lock:
                if not hasattr(cls, '_instance'):
                    TabSensor._instance = super().__new__(cls)
        return TabSensor._instance

    def __del__(self):
        pass

    def init_ui(self, ui):
        self.ui = ui
        self.ui.btn_sensor_open_rev.clicked.connect(self.btn_sensor_open_rev_clicked)
        self.ui.btn_sensor_close_rev.clicked.connect(self.btn_sensor_close_rev_clicked)
        self.group_box_enable(False)

    def group_box_enable(self, b: bool):
        self.ui.groupBox_sensor_charge.setEnabled(b)
        self.ui.groupBox_sensor_change.setEnabled(b)
        self.ui.groupBox_sensor_watering.setEnabled(b)
        self.ui.groupBox_sensor_dust.setEnabled(b)

    def btn_sensor_open_rev_clicked(self):
        Common().set_rev_sensor(True)
        Common().set_status_adc_io(True)
        comManager().send_data(SEND_TYPE.STRING_SEND, self.str_send)
        time.sleep(0.3)
        comManager().send_data(SEND_TYPE.HEX_SEND, 'A5 AA 02 11 66 00')
        time.sleep(0.3)
        comManager().send_data(SEND_TYPE.STRING_SEND, self.str_not_send)
        self.group_box_enable(True)
        pass

    def quit_debug(self):
        comManager().send_data(SEND_TYPE.STRING_SEND, self.str_send)
        time.sleep(0.3)
        comManager().send_data(SEND_TYPE.HEX_SEND, 'A5 AA 02 11 00 00')
        time.sleep(0.3)
        comManager().send_data(SEND_TYPE.STRING_SEND, self.str_not_send)
        time.sleep(0.3)

    def btn_sensor_close_rev_clicked(self):
        Common().set_rev_sensor(False)
        Common().set_status_adc_io(False)
        self.group_box_enable(False)
        self.quit_debug()
        pass

    def update_sensor_status(self, dict_sensor: dict):
        self.update_sensor_io(dict_sensor)
        self.update_sensor_adc(dict_sensor)
        # if Common().get_rev_type() == REV_TYPE.IO_DATA:
        #     self.update_sensor_io(dict_sensor)
        # elif Common().get_rev_type() == REV_TYPE.ADC_DATA:
        #     self.update_sensor_adc(dict_sensor)
        # else:
        #     return

    def update_sensor_io(self, dict_sensor: dict):
        self.change_png(dict_sensor['charge_side_open'], self.ui.t_btn_charge_side_open)
        # self.change_png(dict_sensor['charge_charging'], ui.t_btn_charge_charging)
        self.change_png(dict_sensor['change_up'], self.ui.t_btn_change_up)
        self.change_png(dict_sensor['change_down'], self.ui.t_btn_change_down)
        # self.change_png(dict_sensor['change_center'], ui.t_btn_change_center)
        self.change_png(dict_sensor['change_new_init'], self.ui.t_btn_change_new_init)
        self.change_png(dict_sensor['change_new_work'], self.ui.t_btn_change_new_work)
        self.change_png(dict_sensor['change_old_init'], self.ui.t_btn_change_old_init)
        self.change_png(dict_sensor['change_old_work'], self.ui.t_btn_change_old_work)
        self.change_png(dict_sensor['charge_old_box_on'], self.ui.t_btn_charge_old_box_on)
        self.change_png(dict_sensor['change_new_paste'], self.ui.t_btn_change_new_paste)
        # self.change_png(dict_sensor['change_old_on'], self.ui.t_btn_change_old_on)
        # self.change_png(dict_sensor['change_pad_on'], self.ui.t_btn_change_pad_on)
        # self.change_png(dict_sensor['change_new_mop_on'], self.ui.t_btn_change_new_mop_on)
        # self.change_png(dict_sensor['change_old_mop_on'], ui.t_btn_change_old_mop_on)
        self.change_png(dict_sensor['watering_water_level'], self.ui.t_btn_watering_level)
        self.change_png(dict_sensor['dust_bag_on'], self.ui.t_btn_dust_bag_on)
        self.change_png(dict_sensor['dust_up_open'], self.ui.t_btn_dust_up_cover)
        self.change_png(dict_sensor['dust_drive_error'], self.ui.t_btn_dust_drive_error)

    def update_sensor_adc(self, dict_sensor: dict):
        self.ui.edit_charge_v.setText(str(round(dict_sensor['charge_v'] / 4096 * 3.33 * 11, 2)))
        self.ui.edit_change_clean_on.setText(str(round(dict_sensor['change_clean_on'] / 4096 * 3.3, 2)))
        self.ui.edit_charge_a.setText(str(round(dict_sensor['charge_a'] / 4096 * 3.3 / 11 / 0.1, 2)))
        self.ui.edit_change_old_mop_on.setText(str(round(dict_sensor['change_old_mop_on'] / 4096 * 3.3, 2)))
        self.ui.edit_change_up_down_a.setText(str(round(dict_sensor['change_lift_a'] / 4096 * 3.3 / 11 / 0.1, 2)))
        self.ui.edit_change_new_mop_on.setText(str(round(dict_sensor['change_new_mop_on'] / 4096 * 3.3, 2)))
        self.ui.edit_change_old_move_a.setText(str(round(dict_sensor['change_old_move_a'] / 4096 * 3.3 / 11 / 0.1, 2)))
        self.ui.edit_dust_auto_a.setText(str(round(dict_sensor['dust_auto_a'] / 4096 * 3.3, 2)))
        self.ui.edit_dust_pressure.setText(str(round(dict_sensor['dust_auto_a'], 2)))
        self.ui.edit_watering_auto_a.setText(str(round(dict_sensor['watering_auto_a'] / 4096 * 3.3 / 11 / 0.1, 2)))
        self.ui.edit_watering_leak.setText(str(round(dict_sensor['watering_leak'] / 4096 * 3.3, 2)))
        self.ui.edit_watering_tank_pos.setText(str(round(dict_sensor['watering_tank_pos'] / 4096 * 3.3, 2)))
        self.ui.edit_change_new_move_a.setText(str(round(dict_sensor['change_new_move_a'] / 4096 * 3.3 / 11 / 0.1, 2)))

    @staticmethod
    def change_png(value: int, t_btn):
        if value == 0:
            t_btn.setIcon(QIcon("./res/green.png"))
        else:
            t_btn.setIcon(QIcon("./res/yellow.png"))
